package org.example.pmsystem.service.Impl;

import org.example.pmsystem.entity.Attend;
import org.example.pmsystem.entity.AttendSummary;
import org.example.pmsystem.entity.Salary;
import org.example.pmsystem.mapper.AttendMapper;
import org.example.pmsystem.mapper.SalaryMapper;
import org.example.pmsystem.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Service
public class SalaryServiceImpl implements SalaryService {
    @Autowired
    private SalaryMapper salaryMapper;
    @Autowired
    private AttendMapper attendMapper;

//    private String modify(String status){
//        String tmp = null;
//        switch (status){
//            case "1":
//                tmp="待核算";break;
//            case "2":
//                tmp="待审核";break;
//            case "3":
//                tmp="审核未通过";break;
//            case "4":
//                tmp="待发放";break;
//            case "5":
//                tmp="已发放";break;
//            case "6":
//                tmp="已确认";break;
//            case "7":
//                tmp="有异议";break;
//        }
//        return tmp;
//    }
    @Override
    public List<Salary> selectSalaryBySno(String sno){
        return salaryMapper.selectSalaryBySno(sno);
    }

    @Override
    public List<Salary> selectSalaryByStatus(String status) {
        return salaryMapper.selectSalaryByStatus(status);
    }

    @Override
    public List<Salary> selectSalaryByDate(String date) {
        return salaryMapper.selectSalaryByDate(date);
    }

    private boolean calculate(Salary salary){
        // 缺少基本工资信息，无法计算
        if (salary.getBaseSal()==null || salary.getJobSal()==null) return false;

        // 计算考勤奖金
        AttendSummary summary = attendMapper.selectAttendSumBySnoDate(salary.getSNo(),salary.getSalaryNo());
        if (summary == null){
            summary = new AttendSummary();
            summary.setSignInAmount(0);
            summary.setLateAmount(0);
        }
        float attendSal = 70F*summary.getSignInAmount() - 10*summary.getLateAmount();
        salary.setAttendSal(attendSal);

        // 奖金无则为0
        if (salary.getBonus()==null) salary.setBonus(0F);


/*  假设：上年度当地平均工资：6000
    五险一金：养老保险：8%  医疗保险：2%  失业保险：1%   住房公积金：10%  共21%
    3600
    当个人工资低于社会平均工资的60%时，五险一金的缴纳基数将按照社会月平均工资的60%来计算。
    当个人工资在社会平均工资的60%到300%之间时，五险一金的缴纳基数就是个人的实际工资。
    18000
    当个人工资高于社会平均工资的300%时，五险一金的缴纳基数只会按照社会平均工资的300%来计算，超出部分不计入缴纳基数。
*/
        // 计算税前总工资
        salary.setTotalSal(salary.getBaseSal()+salary.getJobSal()
                +salary.getAttendSal()+salary.getBonus());
        float total = salary.getTotalSal();
        // 计算五险一金
        float avgSal = 6000F, base;
        if (total < avgSal*0.6) base = avgSal*0.6F;
        else if (total > avgSal*3) base = avgSal*3F;
        else base = salary.getTotalSal();
        salary.setSocialSec(base*0.21F);
        // 计算实发工资
        salary.setActualSal(salary.getTotalSal()-salary.getSocialSec());
        // 核算薪资后，更新薪资
        salaryMapper.updateSalary(salary);
        salary.setPaystatus("2");
        updateSalaryStatus(salary);
        return true;
    }

    @Override
    public boolean calculateSalary() {
        boolean tmp = true;
        List<Salary> list = salaryMapper.selectAllSalary();
        for (Salary salary : list)
            if (salary.getPaystatus().equals("1")&&!calculate(salary)) tmp = false;
//            Salary calculate = calculate(salary);
//            if (calculate!=null)
//                salaryMapper.updateSalary(calculate);
//            else tmp = false;
        return tmp;
    }

    @Override
    public void updateSalary(Salary salary) {
        // 更改工资，需要重新核算
        salary.setPaystatus("1");
        updateSalaryStatus(salary);
        salaryMapper.updateSalary(salary);
    }

    @Override
    public void updateSalaryStatus(Salary salary) {
//        modify(status);
        if (salary.getPaystatus().equals("7")){
            salary.setPayTime(LocalDateTime.now());
            salaryMapper.updateSalary(salary);
        }
        salaryMapper.updateSalaryStatus(salary);
    }

    // 考勤
    @Override
    public boolean insertAttend(Attend attend) {
        if (attendMapper.selectAttendBySnoDate(attend) != null)
            return false;

        // 获取日期
        int year,month;
        year = attend.getAttendDate().getYear();
        month = attend.getAttendDate().getMonthValue();
        String result = String.format("%d-%02d",year,month);

        if (attendMapper.selectAttendSumBySnoDate(attend.getSNo(),result) == null)
            attendMapper.insertEmptyAttendSummary(attend.getSNo(),result);

        // 签到
        attendMapper.insertAttend(attend);

        // 添加签到次数
        attendMapper.addSignInTimes(attend.getSNo(), result);

        System.out.println(attend.getSNo()+" "+result);

        // 添加迟到次数
        LocalTime time = attend.getSignIn().toLocalTime();
        if (time.getHour() > 9)
            attendMapper.addlateTimes(attend.getSNo(), result);
        return true;
    }
}
